#!/bin/bash

# FigmaMCPWorkflow 综合验证脚本
# 验证所有优化项是否正确实施

echo "==================================="
echo " FigmaMCPWorkflow 优化验证 v1.0"
echo "==================================="
echo ""

ERRORS=0
WARNINGS=0

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 检查函数
check() {
    if [ $1 -eq 0 ]; then
        echo -e "${GREEN}✓${NC} $2"
    else
        echo -e "${RED}✗${NC} $2"
        ((ERRORS++))
    fi
}

warn() {
    echo -e "${YELLOW}⚠${NC} $1"
    ((WARNINGS++))
}

echo "1. 检查命名一致性..."
echo "------------------------"

# 检查needsPhase4残留
if grep -r "needsPhase4" Workflow/FigmaMCPWorkflow --include="*.md" --exclude-dir="Prompt" | grep -v "副本" > /dev/null 2>&1; then
    check 1 "needsPhase4命名统一"
else
    check 0 "needsPhase4已全部替换为needsCustomPanel"
fi

# 检查validation-report.json残留
if grep -r "validation-report\.json" Workflow/FigmaMCPWorkflow --include="*.md" | grep -v "quality-report" > /dev/null 2>&1; then
    check 1 "validation-report.json命名统一"
else
    check 0 "validation-report.json已全部替换为quality-report.json"
fi

# 检查P2_preview.json残留
if grep -r "P2_preview\.json" Workflow/FigmaMCPWorkflow --include="*.md" | grep -v "P2_preview_build" > /dev/null 2>&1; then
    check 1 "checkpoint命名格式统一"
else
    check 0 "P2_preview.json已替换为P2_preview_build.json"
fi

echo ""
echo "2. 检查文档结构优化..."
echo "------------------------"

# 检查Phase5文档行数
PHASE5_LINES=$(wc -l < Workflow/FigmaMCPWorkflow/Phase5/README.md)
if [ $PHASE5_LINES -lt 200 ]; then
    check 0 "Phase5文档已精简（${PHASE5_LINES}行）"
else
    check 1 "Phase5文档未精简（${PHASE5_LINES}行）"
fi

# 检查脚本文件是否创建
if [ -f "scripts/phase5/static-analysis.js" ]; then
    check 0 "Phase5验证脚本已创建"
else
    check 1 "Phase5验证脚本未创建"
fi

echo ""
echo "3. 检查契约统一性..."
echo "------------------------"

# 检查props-contract示例是否包含类型说明
if grep -q "浮点数0-1" Workflow/FigmaMCPWorkflow/Phase2/README.md; then
    check 0 "props-contract alignmentRate类型已明确"
else
    check 1 "props-contract alignmentRate类型未明确"
fi

# 检查validate-phase-gate.ts是否正确处理alignmentRate
if grep -q "alignmentRate \* 100" Workflow/FigmaMCPWorkflow/scripts/validate-phase-gate.ts 2>/dev/null; then
    check 0 "validate-phase-gate正确处理alignmentRate"
else
    warn "validate-phase-gate.ts处理alignmentRate需要验证"
fi

echo ""
echo "4. 检查资源集中管理..."
echo "------------------------"

# 检查是否存在重复模板
TEMPLATE_COUNT=$(find Workflow/FigmaMCPWorkflow -name "mergeLocalComponents.template.ts" -type f | wc -l)
if [ $TEMPLATE_COUNT -eq 1 ]; then
    check 0 "模板文件已去重（仅1份）"
else
    check 1 "存在重复模板文件（${TEMPLATE_COUNT}份）"
fi

# 检查Phase5/templates目录是否已删除
if [ -d "Workflow/FigmaMCPWorkflow/Phase5/templates" ]; then
    check 1 "Phase5/templates目录未删除"
else
    check 0 "Phase5/templates目录已删除"
fi

echo ""
echo "5. 检查Preview-first契约..."
echo "------------------------"

# 检查Phase3是否明确Preview-first
if grep -q "唯一事实源" Workflow/FigmaMCPWorkflow/Phase3/README.md; then
    check 0 "Phase3已实施Preview-first契约"
else
    check 1 "Phase3未明确Preview-first契约"
fi

# 检查preview-props-schema模板是否创建
if [ -f "Workflow/FigmaMCPWorkflow/Phase2/preview-props-schema.template.json" ]; then
    check 0 "preview-props-schema模板已创建"
else
    check 1 "preview-props-schema模板未创建"
fi

echo ""
echo "6. 检查Phase间引用..."
echo "------------------------"

# 检查是否正确使用blueprint-aligned.json
if grep -q "blueprint-aligned\.json.*关键.*对齐后" Workflow/FigmaMCPWorkflow/Phase2/README.md; then
    check 0 "Phase2正确引用blueprint-aligned.json"
else
    warn "Phase2引用blueprint-aligned需要确认"
fi

echo ""
echo "==================================="
echo " 验证结果汇总"
echo "==================================="
echo ""

if [ $ERRORS -eq 0 ]; then
    echo -e "${GREEN}✅ 所有优化项验证通过！${NC}"

    if [ $WARNINGS -gt 0 ]; then
        echo -e "${YELLOW}⚠ 存在 $WARNINGS 个警告需要关注${NC}"
    fi

    echo ""
    echo "优化成功完成的项目："
    echo "  ✓ 命名统一化"
    echo "  ✓ 文档结构优化"
    echo "  ✓ 契约结构统一"
    echo "  ✓ 资源集中管理"
    echo "  ✓ Preview-first契约"
    echo "  ✓ Phase间引用修复"

    exit 0
else
    echo -e "${RED}❌ 发现 $ERRORS 个错误需要修复${NC}"

    if [ $WARNINGS -gt 0 ]; then
        echo -e "${YELLOW}⚠ 另有 $WARNINGS 个警告需要关注${NC}"
    fi

    echo ""
    echo "请检查上述失败项并进行修复。"

    exit 1
fi